<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.leaderId" placeholder="教师编号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search">
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      style="width: 100%; margin-top:10px;"
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="150" prop="id">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="赛事名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.matchName }}
        </template>
      </el-table-column>
      <el-table-column label="组赛单位" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.matchUnit }}
        </template>
      </el-table-column>
      <el-table-column label="赛制" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.matchRules === 0">
            个人赛
          </div>
          <div v-else>
            团队赛
          </div>
        </template>
      </el-table-column>
      <el-table-column label="项目负责人" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.subLeader }}
        </template>
      </el-table-column>
      <el-table-column label="联系电话" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.telephone }}
        </template>
      </el-table-column>
      <el-table-column label="电子邮箱" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.subStatus === 0" type="primary" size="mini" @click="handleUpdate(scope.row.subId)">
            审核
          </el-button>
          <el-button type="primary" size="mini" @click="handleView(scope.row.subId)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <div>
        <span>项目详情</span>
        <table style="width:100%;" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
          <tbody>
            <tr>
              <td>赛事名称</td>
              <td>{{ temp.matchName }}</td>
            </tr>
            <tr>
              <td>组赛单位</td>
              <td>{{ temp.matchUnit }}</td>
            </tr>
            <tr>
              <td>赛制</td>
              <td>{{ temp.matchRules }}</td>
            </tr>
            <tr>
              <td>项目负责人</td>
              <td>{{ temp.subLeader }}</td>
            </tr>
            <tr>
              <td>联系电话</td>
              <td>{{ temp.telephone }}</td>
            </tr>
            <tr>
              <td>电子邮箱</td>
              <td>{{ temp.email }}</td>
            </tr>
            <tr>
              <td>竞赛开始时间</td>
              <td>{{ temp.matchStartTime }}</td>
            </tr>
            <tr>
              <td>竞赛结束时间</td>
              <td>{{ temp.matchEndTime }}</td>
            </tr>
            <tr>
              <td>专业</td>
              <td>{{ temp.major }}</td>
            </tr>
            <tr>
              <td>竞赛主办单位</td>
              <td>{{ temp.matchHostUnit }}</td>
            </tr>
            <tr>
              <td>竞赛承办单位</td>
              <td>{{ temp.matchUndertakeUnit }}</td>
            </tr>
            <tr>
              <td>立项日期
              </td>
              <td>{{ temp.subTime }}
              </td>
            </tr>
            <tr>
              <td>论证目的和意义</td>
              <td>{{ temp.meaning }}</td>
            </tr>
            <tr>
              <td>通知附件</td>
              <td>{{ temp.matchFile }}</td>
            </tr>
            <tr>
              <td>立项申请书</td>
              <td>{{ temp.subFile }}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div style="margin-top:30px">
        <span>预算资金</span>
        <table style="width:100%;" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000">
          <tbody>
            <tr>
              <td>注册费</td>
              <td>{{ temp.free.registerFree }}</td>
            </tr>
            <tr>
              <td>差旅费</td>
              <td>{{ temp.free.businessFree }}</td>
            </tr>
            <tr>
              <td>培训费</td>
              <td>{{ temp.free.trainFree }}</td>
            </tr>
            <tr>
              <td>评审费</td>
              <td>{{ temp.free.reviewFree }}</td>
            </tr>
            <tr>
              <td>领队费</td>
              <td>{{ temp.free.leaderFree }}</td>
            </tr>
            <tr>
              <td>组织费</td>
              <td>{{ temp.free.organizeFree }}</td>
            </tr>
            <tr>
              <td>指导费</td>
              <td>{{ temp.free.guideFree }}</td>
            </tr>
            <tr>
              <td>材料费</td>
              <td>{{ temp.free.materialFree }}</td>
            </tr>
            <tr>
              <td>教师奖金</td>
              <td>{{ temp.free.teacherBonus }}</td>
            </tr>
            <tr>
              <td>合计费用</td>
              <td>{{ temp.free.sumFree }}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 详情的表单 -->
      <div v-if="dialogStatus === 'view'" style="margin-top:30px">
        <el-form ref="dataForm" :model="temp" :rules="formRules" label-position="left" label-width="70px" style="width: 500px; margin-left: 50px;">
          <el-form-item label="意见" prop="opinion" label-width="120px">
            <el-input v-model="temp.opinion" type="textarea" disabled="true" />
          </el-form-item>
          <el-form-item label="审核状态">
            <el-radio-group v-model="temp.subStatus">
              <el-radio :label="1" disabled="true">通过</el-radio>
              <el-radio :label="2" disabled="true">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <!-- 审核的表单 -->
      <div v-if="dialogStatus === 'update'" style="margin-top:30px">
        <el-form ref="dataForm" :model="form" :rules="formRules" label-position="left" label-width="70px" style="width: 500px; margin-left: 50px;">
          <el-form-item label="意见" prop="opinion" label-width="120px">
            <el-input v-model="form.opinion" type="textarea" />
          </el-form-item>
          <el-form-item label="审核状态">
            <el-radio-group v-model="form.subStatus">
              <el-radio label="1">通过</el-radio>
              <el-radio label="2">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button v-if="dialogStatus === 'update'" type="primary" @click="updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { adminGetList, getOne, review } from '@/api/subject'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    const validatePowerId = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('权限编号不能为空'))
      } else {
        callback()
      }
    }
    const validatePowerName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('权限名称不能为空'))
      } else {
        callback()
      }
    }
    const validateRePath = (rule, value, callback) => {
      if (this.dialogStatus === 'create' && value === '') {
        callback(new Error('权限路径不能为空'))
      } else {
        callback()
      }
    }
    const validateMethod = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请求类型不能为空'))
      } else {
        callback()
      }
    }
    return {
      formRules: {
        powerId: [{ required: true, trigger: 'blur', validator: validatePowerId }],
        powerName: [{ required: true, trigger: 'blur', validator: validatePowerName }],
        resourcePath: [{ required: true, trigger: 'blur', validator: validateRePath }],
        method: [{ required: true, trigger: 'blur', validator: validateMethod }]
      },
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        leaderId: undefined
      },
      list: null,
      total: 0,
      listLoading: true,
      textMap: {
        update: '详情',
        create: '添加'
      },
      dialogStatus: '',
      dialogFormVisible: false,
      temp: {
        id: '',
        subId: '',
        matchName: '',
        matchUnit: '',
        matchRules: '',
        subLeader: '',
        telephone: '',
        email: '',
        matchStartTime: '',
        matchEndTime: '',
        major: '',
        matchHostUnit: '',
        matchUndertakeUnit: '',
        meaning: '',
        matchFile: '',
        subFile: '',
        free: ''
      },
      form: {
        subId: '',
        opinion: '',
        subStatus: undefined
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNum = 1
      if (this.listQuery.title.length === 0) {
        this.listQuery.title = undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      adminGetList(this.listQuery).then(response => {
        this.list = response.data.content
        this.total = response.data.totalSize
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        subId: '',
        matchName: '',
        matchUnit: '',
        matchRules: '',
        subLeader: '',
        telephone: '',
        email: '',
        matchStartTime: '',
        matchEndTime: '',
        major: '',
        matchHostUnit: '',
        matchUndertakeUnit: '',
        meaning: '',
        matchFile: '',
        subFile: '',
        free: ''
      }
    },
    resetForm() {
      this.form = {
        subId: '',
        opinion: '',
        subStatus: undefined
      }
    },
    handleUpdate(subId) {
      this.resetTemp()
      this.resetForm()
      getOne(subId).then(response => {
        this.temp = response.data
      })
      this.form.subId = subId
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          review(this.form).then(response => {
            const index = this.list.findIndex(v => v.subId === this.form.subId)
            this.list.splice(index, 1, response.data)
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: '审核成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleView(subId) {
      this.resetTemp()
      this.resetForm()
      getOne(subId).then(response => {
        this.temp = response.data
      })
      this.dialogStatus = 'view'
      this.dialogFormVisible = true
    }
  }
}
</script>
